package cn.yunhe.util;

import cn.yunhe.entity.ExamPaper;
import cn.yunhe.entity.Question;
import cn.yunhe.service.ExamPaperService;
import freemarker.template.TemplateException;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

public class DownloadExamPaper {


    public static String makePaper(Integer examPaperId,List<Question> allQuestionList,ExamPaper examPaper,String savePath){
        Map<String, Object> data = new HashMap<>();
        List<Question> questionList = new ArrayList<>();
        List<Question> judgmentQuestionList = new ArrayList<>();
        //筛选出选择题列表
        for (Question aql : allQuestionList) {
            if (aql.getQuestionType().getId() == 1) {
                questionList.add(aql);
            }
        }
        //筛选出判断题
        for (Question aql : allQuestionList) {
            if (aql.getQuestionType().getId() == 3) {
                judgmentQuestionList.add(aql);
            }
        }
        //封装题类型
        data.put("examPaper", examPaper);
        data.put("chooseQuestions", questionList);
        data.put("judges", judgmentQuestionList);
        //生成下载文件
        try {
            StringBuilder excelPath = new StringBuilder();
            excelPath.append(savePath+"/static/resources/template/examPaper");
            String pathcontain = UUID.randomUUID().toString();
            System.out.println(pathcontain);
            excelPath.append(pathcontain);
            excelPath.append(".doc");
            System.out.println(excelPath.toString());
            TemplateParseUtil.parse("/usr/java/tomcat8/webapps/ROOT/WEB-INF/classes/static/resources/orginTemp", "exampaperTemp.ftl", excelPath.toString(), data);
            return "../resources/template/"+"examPaper"+pathcontain+".doc";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "123";
    }


}
